﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SuperFox.SuperServiceReference;


namespace SuperFox
{
    public partial class Help : UserControl
    {

        private SuperServiceClient client;

        public Help()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            client = ServiceConfig.Connect();

            client.GetHelpItemsCompleted += new EventHandler<GetHelpItemsCompletedEventArgs>(client_GetHelpItemsCompleted);

        }

        void client_GetHelpItemsCompleted(object sender, GetHelpItemsCompletedEventArgs e)
        {
            // add the help items to the tree view

            if (e.Error == null)
            {
                foreach (DbHelp hItem in e.Result)
                {
                    InsertHelpItem(hItem, TopLevelItem);
                }
            }
        }

        /// <summary>
        /// Time for some recursion
        /// </summary>
        /// <param name="item"></param>
        /// <param name="container"></param>
        private void InsertHelpItem(DbHelp item, TreeViewItemExtension container)
        {

            if (container.ID == item.ParentID)
            {

                // make sure this item isnt in there already

                HypperLinkButtonExtension titleLink = new HypperLinkButtonExtension()
                {
                    Content = item.Title,
                    IsTabStop = false,
                    HelpString = item.ContentText
                };

                titleLink.Click += new RoutedEventHandler(titleLink_Click);
                

                TreeViewItemExtension addItem = new TreeViewItemExtension()
                {
                    Header = titleLink,
                    ID = item.index,
                    HelpItem = item,
                    IsTabStop = false
                };

               

                if (!container.ContainsHelpItem(item))
                {
                    container.Items.Add(addItem);

                    addItem.Expanded += new RoutedEventHandler(TopLevelItem_Expanded);

                    // give the new item a dummy loading item so it can be expanded

                    TreeViewItemExtension loadingItem = new TreeViewItemExtension()
                    {
                        Header = "Loading ..."
                    };

                    addItem.Items.Add(loadingItem);
                }

            }
            else
            {
                if (container.Items != null)
                {
                    foreach (TreeViewItemExtension its in container.Items)
                    {
                        InsertHelpItem(item, its);
                    }
                }
            }
        }

        void titleLink_Click(object sender, RoutedEventArgs e)
        {
            ContentViewer.Text = (sender as HypperLinkButtonExtension).HelpString;
        }

        /// <summary>
        /// handler for all the expanded item events
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TopLevelItem_Expanded(object sender, RoutedEventArgs e)
        {
            if (!(sender as TreeViewItemExtension).IsLoaded)
            {
                (sender as TreeViewItemExtension).Items.Clear();
                (sender as TreeViewItemExtension).IsLoaded = true;
                client.GetHelpItemsAsync((sender as TreeViewItemExtension).ID);
            }
        }
    }
}
